<template>
  <div class="payment-container">
    <!-- 头部 -->
    <div class="header">
      <button class="back-button" @click="navigateBack">返回</button>
      <span class="header-title">服务下单</span>
      <div class="header-right"></div>
    </div>

    <!-- 企业信息卡片 -->
    <div class="company-info-card">
      <span class="company-name fixed-font-size">{{ orderInfo.company_name }}</span>
      <span class="credit-code fixed-font-size">统一信用代码：{{ orderInfo.credit_code }}</span>
      <span class="legal-person fixed-font-size">法定代表人：{{ orderInfo.legal_person }}</span>
    </div>

    <!-- 服务选择 -->
    <div class="service-selection">
      <span class="section-title fixed-font-size">请选择服务</span>
      <div class="service-options">
        <div 
          v-for="service in serviceList" 
          :key="service.id" 
          class="service-option" 
          :class="{ 'selected': selectedService === service.id }" 
          @click="selectService(service.id)"
        >
          <span class="service-name fixed-font-size">{{ service.name }}</span>
          <span class="service-price fixed-font-size">{{ service.price_display }}</span>
        </div>
      </div>
    </div>

    <!-- 联系方式 -->
    <div class="contact-info">
      <span class="contact-label fixed-font-size">*联系电话：</span>
      <input type="tel" v-model="contactPhone" placeholder="输入手机号码" class="contact-input" />
    </div>

    <!-- 服务描述 -->
    <div class="service-process" v-if="selectedServiceInfo">
      <span class="process-title fixed-font-size">{{ selectedServiceInfo.name }}服务说明</span>
      <div class="process-content">
        <span class="process-text fixed-font-size">{{ selectedServiceInfo.description }}</span>
      </div>
    </div>

    <!-- 底部下单按钮 -->
    <div class="bottom">
      <button class="btn-order" @click="submitOrder" :disabled="isLoading || !contactPhone">
        {{ isLoading ? '处理中...' : '立即下单' }}
      </button>
    </div>

    <!-- 加载提示 -->
    <div class="loading-mask" v-if="isLoading">
      <div class="loading-content">
        <div class="loading-spinner"></div>
        <span class="loading-text">正在处理...</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ActiveList, createOrder } from '@/api'

const router = useRouter()
const route = useRoute()

// 响应式数据
const orderInfo = ref({
  company_name: '北京星路软件有限公司',
  credit_code: '91110116MA01B5MT9N',
  legal_person: '连达海'
})

const serviceList = ref([])
const selectedService = ref(null)
const contactPhone = ref('')
const isLoading = ref(false)

// 计算属性：获取选中的服务信息
const selectedServiceInfo = computed(() => {
  return serviceList.value.find(service => service.id === selectedService.value)
})

// 生命周期钩子
onMounted(() => {
  // 使用Vue Router的route.query获取参数
  console.log('路由参数:', route.query)
  
  // 从路由参数获取公司信息
  const companyName = route.query.companyName
  const creditCode = route.query.creditCode
  const legalPerson = route.query.legalPerson
  
  // 如果URL参数存在，则更新订单信息
  if (companyName) {
    orderInfo.value.company_name = companyName
  }
  if (creditCode) {
    orderInfo.value.credit_code = creditCode
  }
  if (legalPerson) {
    orderInfo.value.legal_person = legalPerson
  }
  
  // 获取服务列表
  fetchServiceList()
})

// 获取服务列表
const fetchServiceList = async () => {
  try {
    isLoading.value = true
    const res = await ActiveList()
    
    if (res.code === 1) {
      serviceList.value = res.data
      // 默认选择第一个服务
      if (serviceList.value.length > 0) {
        selectedService.value = serviceList.value[0].id
      }
    } else {
      console.error('获取服务列表失败:', res.msg)
      alert('获取服务列表失败，请稍后重试')
    }
  } catch (error) {
    console.error('获取服务列表异常:', error)
    alert('网络错误，请检查网络连接')
  } finally {
    isLoading.value = false
  }
}

// 返回上一页
const navigateBack = () => {
  router.back()
}

// 选择服务
const selectService = (serviceId) => {
  selectedService.value = serviceId
}

// 提交订单
const submitOrder = async () => {
  if (!contactPhone.value) {
    alert('请输入联系电话')
    return
  }
  
  if (!selectedService.value) {
    alert('请选择服务项目')
    return
  }
  
  isLoading.value = true
  
  try {
    // 获取选中的服务信息
    const serviceInfo = serviceList.value.find(service => service.id === selectedService.value)
    
    // 获取当前时间作为注册时间
    const registerTime = new Date().toISOString().split('T')[0] // 格式: YYYY-MM-DD
    
    // 调用创建订单接口
    const orderRes = await createOrder({
      company_name: orderInfo.value.company_name,
      credit_code: orderInfo.value.credit_code,
      legal_person: orderInfo.value.legal_person,
      service_item: serviceInfo.name,
      service_fee: serviceInfo.price,
      contact_phone: contactPhone.value,
      register_time: registerTime
    })
    
    if (orderRes.code === 1) {
      // 创建订单成功，跳转到收银台页面
      // 使用路由参数传递，而不是URL参数
      router.push({
        path: '/pages/cashier/index',
        query: {
          orderSn: orderRes.data.order_sn,
          serviceFee: orderRes.data.service_fee
        }
      })
    } else {
      alert('创建订单失败: ' + (orderRes.msg || '未知错误'))
    }
  } catch (error) {
    console.error('提交订单失败:', error)
    alert('提交订单失败，请稍后重试')
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
/* 样式保持不变，与之前相同 */
.payment-container {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 80px;
  font-size: 16px;
  line-height: 1.5;
}

.fixed-font-size {
  font-size: inherit !important;
  line-height: inherit !important;
}

.header {
  background-color: #1890ff;
  color: white;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: sticky;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  font-size: 16px;
}

.header-title {
  font-size: 18px;
  font-weight: bold;
  color: white;
  text-align: center;
  flex: 1;
}

.back-button {
  background: none;
  border: none;
  color: white;
  font-size: 16px;
  padding: 8px 12px;
  width: auto;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.header-right {
  width: 40px;
}

.company-info-card {
  background: linear-gradient(to bottom, #1890ff, #40a9ff);
  color: white;
  padding: 20px 15px;
  font-size: 16px;
}

.company-name {
  font-size: 18px;
  font-weight: bold;
  display: block;
  margin-bottom: 10px;
}

.credit-code {
  font-size: 14px;
  display: block;
  margin-bottom: 5px;
  opacity: 0.9;
}

.legal-person {
  font-size: 14px;
  display: block;
  opacity: 0.9;
}

.service-selection {
  background-color: #ffffff;
  margin: 10px;
  border-radius: 8px;
  padding: 15px;
  font-size: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 15px;
  display: block;
}

.service-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.service-option {
  border: 1px solid #d9d9d9;
  border-radius: 8px;
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 16px;
}

.service-option:hover {
  border-color: #1890ff;
  background-color: #f5f8ff;
}

.service-option.selected {
  border-color: #1890ff;
  background-color: #e6f7ff;
}

.service-name {
  font-size: 16px;
  color: #333333;
}

.service-price {
  font-size: 18px;
  font-weight: bold;
  color: #1890ff;
}

.contact-info {
  background-color: #ffffff;
  margin: 10px;
  border-radius: 8px;
  padding: 15px;
  display: flex;
  align-items: center;
  font-size: 16px;
}

.contact-label {
  font-size: 16px;
  color: #333333;
  margin-right: 10px;
  white-space: nowrap;
}

.contact-input {
  flex: 1;
  height: 40px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  padding: 0 10px;
  font-size: 16px;
}

.contact-input:focus {
  border-color: #1890ff;
  outline: none;
}

.service-process {
  background-color: #ffffff;
  margin: 10px;
  border-radius: 8px;
  padding: 15px;
  font-size: 16px;
}

.process-title {
  font-size: 16px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 15px;
  display: block;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.process-content {
  margin-bottom: 15px;
}

.process-text {
  font-size: 14px;
  color: #666666;
  line-height: 1.6;
  display: block;
}

.loading-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-content {
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 20px 30px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.loading-spinner {
  width: 30px;
  height: 30px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 14px;
}

.bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #ffffff;
  padding: 15px;
  border-top: 1px solid #eeeeee;
}

.btn-order {
  background-color: #1890ff;
  color: #ffffff;
  border: none;
  width: 100%;
  height: 48px;
  border-radius: 4px;
  font-size: 18px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-order:hover:not(:disabled) {
  background-color: #0d7de0;
}

.btn-order:disabled {
  background-color: #d9d9d9;
  color: #ffffff;
  cursor: not-allowed;
}

@media screen and (max-width: 375px) {
  .payment-container {
    font-size: 14px;
  }
  
  .company-name {
    font-size: 16px;
  }
  
  .service-name {
    font-size: 14px;
  }
  
  .service-price {
    font-size: 16px;
  }
}

@media screen and (min-width: 768px) {
  .payment-container {
    max-width: 480px;
    margin: 0 auto;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    font-size: 16px;
  }
  
  .header {
    max-width: 480px;
    margin: 0 auto;
    left: 0;
    right: 0;
    position: sticky;
  }
  
  .bottom {
    max-width: 480px;
    margin: 0 auto;
    left: 0;
    right: 0;
  }
}
</style>